package jlb.andriva.wrappers;

import android.content.Context;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;

import jlb.andriva.R;

public class Rates {

    private ArrayList<ArrayList<String>> mData = new ArrayList<ArrayList<String>>();
    private String mMsg;

    public Rates(Element root){
        Elements trs = root.getElementsByClass("coloredRows");
        for(int i=0;i<trs.size();i++){
            Element tr = trs.get(i);
            Elements tds = tr.select("td");
            mData.add(new ArrayList<String>());
            for(int j=0;j<tds.size();j++){
                mData.get(i).add(tds.get(j).text().trim());
            }
        }
        Elements tds = root.select("td[align=center]");
        mMsg = tds.get(tds.size()-1).text().trim();
    }

    public LinearLayout getView(Context context){
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.weight = 1;
        params.setMargins(2, 2, 2, 2);

        LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        mainParams.setMargins(2, 8, 2, 8);

        LinearLayout main = new LinearLayout(context);
        main.setLayoutParams(mainParams);
        //main.setBackgroundResource(R.drawable.border);
        main.setOrientation(LinearLayout.VERTICAL);

        LinearLayout titles = new LinearLayout(context);
        titles.setOrientation(LinearLayout.HORIZONTAL);
        titles.setLayoutParams(params);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

        TextView lblTitMes = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblTitMes.setGravity(Gravity.CENTER);
        lblTitMes.setEllipsize(TextUtils.TruncateAt.END);
        lblTitMes.setLayoutParams(params);
        lblTitMes.setTypeface(null, Typeface.BOLD);
        lblTitMes.setText("Tasa");

        titles.addView(lblTitMes);

        TextView lblTitDia = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblTitDia.setGravity(Gravity.CENTER);
        lblTitDia.setEllipsize(TextUtils.TruncateAt.END);
        lblTitDia.setLayoutParams(params);
        lblTitDia.setTypeface(null, Typeface.BOLD);
        lblTitDia.setText("$");

        titles.addView(lblTitDia);

        main.addView(titles);

        LinearLayout.LayoutParams paramsRuler = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 2);

        View ruler = inflater.inflate(R.layout.ruler, null);
        ruler.setLayoutParams(paramsRuler);

        main.addView(ruler);

        for(int i=0;i<mData.size();i++){
            LinearLayout data = new LinearLayout(context);
            data.setOrientation(LinearLayout.HORIZONTAL);
            data.setLayoutParams(params);

            TextView lblMes = (TextView) inflater.inflate(R.layout.tvnormal, null);
            lblMes.setEllipsize(TextUtils.TruncateAt.END);
            lblMes.setLayoutParams(params);
            lblMes.setGravity(Gravity.CENTER);
            lblMes.setText(mData.get(i).get(0));

            data.addView(lblMes);

            TextView lblDia = (TextView) inflater.inflate(R.layout.tvnormal, null);
            lblDia.setEllipsize(TextUtils.TruncateAt.END);
            lblDia.setLayoutParams(params);
            lblDia.setGravity(Gravity.CENTER);
            lblDia.setText(mData.get(i).get(1));

            data.addView(lblDia);

            main.addView(data);
        }

        View ruler2 = inflater.inflate(R.layout.ruler, null);
        ruler2.setLayoutParams(paramsRuler);

        main.addView(ruler2);

        TextView lblMsg = (TextView) inflater.inflate(R.layout.tvnormal, null);
        lblMsg.setLayoutParams(params);
        lblMsg.setEllipsize(TextUtils.TruncateAt.END);
        lblMsg.setText(mMsg);

        main.addView(lblMsg);

        return main;
    }
}
